package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IUserLoginService;
import com.easy.common.util.PasswordUtil;
import com.easy.common.util.SYS;
import com.easy.common.util.TokenUtil;
import com.easy.common.zhu.LogMethod;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@DubboService
public class UserLoginServiceImpl implements IUserLoginService {
    @Autowired
    UserMapper userMapper;

    @Override
    @LogMethod(serviceName = "用户登录")
    public String login(UserBean userBean) {
        UserBean result;

        // 判断是通过手机号登录还是ID登录
        if (userBean.getPhone() != null && !userBean.getPhone().isEmpty()) {
            // 通过手机号查询用户
            LambdaQueryWrapper<UserBean> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserBean::getPhone, userBean.getPhone());
            result = userMapper.selectOne(queryWrapper);
        } else if (userBean.getId() != null) {
            // 通过ID查询用户
            result = userMapper.selectById(userBean.getId());
        } else {
            throw new RuntimeException("请提供手机号或用户ID");
        }

        if (result == null) {
            throw new RuntimeException("用户不存在");
        }

        // 验证密码
        if(!PasswordUtil.checkPassword(userBean.getPassword(), result.getPassword())){
            throw new RuntimeException("密码错误");
        }

        // 返回token，标记用户类型
        return TokenUtil.getToken(result.getId().toString(), "user");
    }

    @Override
    public UserBean logininfo(String token) {
        Object id = TokenUtil.getOption(token,"MANAGER_ID");
        System.out.println(id);
        if(id == null){
            throw new RuntimeException("登录异常:token中无用户信息");
        }
        return userMapper.selectById(Integer.parseInt(id.toString()));
    }

    // 新增：用户注册方法
    @Override
    public int register(UserBean user) {
        // 检查手机号是否已存在
        LambdaQueryWrapper<UserBean> phoneQuery = new LambdaQueryWrapper<>();
        phoneQuery.eq(UserBean::getPhone, user.getPhone());
        UserBean existingUser = userMapper.selectOne(phoneQuery);
        if (existingUser != null) {
            throw new RuntimeException("该手机号已注册");
        }

        // 检查身份证号是否已存在
        LambdaQueryWrapper<UserBean> idCardQuery = new LambdaQueryWrapper<>();
        idCardQuery.eq(UserBean::getIdcard, user.getIdcard());
        existingUser = userMapper.selectOne(idCardQuery);
        if (existingUser != null) {
            throw new RuntimeException("该身份证号已注册");
        }

        // 加密密码
        user.setPassword(PasswordUtil.getEncrypyion(user.getPassword()));

        // 保存用户
        return userMapper.insert(user);
    }

    // 新增：重置密码方法
    @Override
    public int resetPassword(String phone, String realname, String newPassword) {
        // 通过手机号查询用户
        LambdaQueryWrapper<UserBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBean::getPhone, phone);
        UserBean user = userMapper.selectOne(queryWrapper);

        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 验证真实姓名是否匹配
        if (!realname.equals(user.getRealname())) {
            throw new RuntimeException("真实姓名不匹配");
        }

        // 更新密码
        user.setPassword(PasswordUtil.getEncrypyion(newPassword));
        return userMapper.updateById(user);
    }
}






































